/**
 * 
 */
package cn.edu.pku.sei.ra.ui;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import ac.jp.u_tokyo.QVTWrapper.SynchronizationFailedException;

import cn.edu.pku.sei.ra.framework.CausalConnector;

/**
 * @author songhui
 *
 */
public class CreateConnectorDialog extends Dialog {
	
	Combo workingProject=null;
	//Combo tempDir=null;
	Combo qvt=null;
	Combo sysMeta=null;
	Combo arcMeta=null;
	Combo sysMetaURI=null;
	Combo arcMetaURI=null;
	Combo arcFile=null;
	Text transName=null;
	Text arcName=null;
	Text sysName=null;
	
	IWorkspaceRoot root=null;
	IProject project=null;
	
	CausalConnector cc=new CausalConnector();

	/**
	 * @param parentShell
	 */
	public CreateConnectorDialog(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param parentShell
	 */
	public CreateConnectorDialog(IShellProvider parentShell) {
		super(parentShell);
		
	}
	
	protected Control createDialogArea(Composite parent) {
		parent=(Composite)super.createDialogArea(parent);
		GridLayout gdlayout=new GridLayout(3,false);
		GridData gddata=null;
		parent.setLayout(gdlayout);
		
		gddata=new GridData(GridData.FILL_VERTICAL);
		gddata.widthHint=300;
		gddata.heightHint=20;
		gddata.horizontalSpan=3;
		
		Label label=null;
		
		label=new Label(parent,SWT.LEFT);
		label.setText("Select a project for environment");
		label.setLayoutData(gddata);
		workingProject=new Combo(parent,SWT.LEFT);		
		workingProject.setLayoutData(gddata);
		
		
		
		label=new Label(parent,SWT.LEFT);
		label.setText("Select the architecture meta model");
		label.setLayoutData(gddata);
		arcMeta=new Combo(parent,SWT.LEFT);
		arcMeta.setLayoutData(gddata);
		
		label=new Label(parent,SWT.LEFT);
		label.setText("Select the registered URI for architecture meta model");
		label.setLayoutData(gddata);
		arcMetaURI=new Combo(parent,SWT.LEFT);
		arcMetaURI.setLayoutData(gddata);
		
		label=new Label(parent,SWT.LEFT);
		label.setText("Select the system meta model");
		label.setLayoutData(gddata);
		sysMeta=new Combo(parent,SWT.LEFT);
		sysMeta.setLayoutData(gddata);
		
		label=new Label(parent,SWT.LEFT);
		label.setText("Select the registered URI for system meta model");
		label.setLayoutData(gddata);
		sysMetaURI=new Combo(parent,SWT.LEFT);
		sysMetaURI.setLayoutData(gddata);
		
		label=new Label(parent,SWT.LEFT);
		label.setText("Select the architecture file");
		label.setLayoutData(gddata);
		arcFile=new Combo(parent,SWT.LEFT);
		arcFile.setLayoutData(gddata);
		
				
		label=new Label(parent,SWT.LEFT);
		label.setText("Select the QVT source file");
		label.setLayoutData(gddata);
		qvt=new Combo(parent,SWT.LEFT);
		qvt.setLayoutData(gddata);
		
		
		GridData gdsmall=new GridData(GridData.FILL_VERTICAL);
		gdsmall.heightHint=20;
		gdsmall.widthHint=80;
		gdsmall.horizontalSpan=1;
		
		label=new Label(parent,SWT.LEFT);
		label.setText("Transformation name");
		label.setLayoutData(gdsmall);
		
		
		label=new Label(parent,SWT.LEFT);
		label.setText("Architecture name");
		label.setLayoutData(gdsmall);
		
		label=new Label(parent,SWT.LEFT);
		label.setText("System name");
		label.setLayoutData(gdsmall);
		
		transName=new Text(parent,SWT.LEFT);
		transName.setLayoutData(gdsmall);
		
		arcName=new Text(parent,SWT.LEFT);
		arcName.setLayoutData(gdsmall);
		
		sysName=new Text(parent,SWT.LEFT);
		sysName.setLayoutData(gdsmall);
		
		label=new Label(parent,SWT.LEFT);
		label.setLayoutData(gdsmall);
		Button btSave=new Button(parent,SWT.LEFT);
		btSave.setLayoutData(gdsmall);
		Button btLoad=new Button(parent,SWT.LEFT);
		btLoad.setLayoutData(gdsmall);
		
		final Composite fparent=parent;
		btSave.setText("Save");
		btSave.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				FileDialog fd=new FileDialog(fparent.getShell(),SWT.SAVE);
				String path=fd.open();
				if(path==null || path.length()<1)
					return;
				try {
					FileOutputStream fos=new FileOutputStream(path);
					PrintStream out=new PrintStream(fos);
					out.println(workingProject.getText());
					out.println(arcMeta.getText());
					out.println(arcMetaURI.getText());
					out.println(sysMeta.getText());
					out.println(sysMetaURI.getText());
					out.println(arcFile.getText());
					out.println(qvt.getText());
					out.println(transName.getText());
					out.println(arcName.getText());
					out.println(sysName.getText());
					out.close();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		btLoad.setText("Load");
		btLoad.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				FileDialog fd=new FileDialog(fparent.getShell(),SWT.OPEN);
				String path=fd.open();
				if(path==null || path.length()<1)
					return;
				try {
					FileInputStream fos=new FileInputStream(path);
					BufferedReader rd=new BufferedReader(new InputStreamReader(fos));
					workingProject.setText(rd.readLine());
					arcMeta.setText(rd.readLine());
					arcMetaURI.setText(rd.readLine());
					sysMeta.setText(rd.readLine());
					sysMetaURI.setText(rd.readLine());
					arcFile.setText(rd.readLine());
					qvt.setText(rd.readLine());
					transName.setText(rd.readLine());
					arcName.setText(rd.readLine());
					sysName.setText(rd.readLine());
					rd.close();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				
			}
		});
		
		
		workingProject.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				initFiles();
			}
		});
		
		root=ResourcesPlugin.getWorkspace().getRoot();
		initWorkingProject();
		initPackage();
	
/*		arcFile.setText("test.c2");
		arcMeta.setText("C2.ecore");
		arcMetaURI.setText("http://www.sei.pku.edu.cn/rsa/c2");
		sysMeta.setText("JOnAS.ecore");
		sysMetaURI.setText("http://www.sei.pku.edu.cn/rsa/jonas");
		arcName.setText("arc");
		sysName.setText("sys");
		transName.setText("C2JOnAS");
*/
/*		
		arcFile.setText("My.simplegui");
		arcMeta.setText("SimpleGUI.ecore");
		arcMetaURI.setText("http://www.sei.pku.edu.cn/rsa/simplegui");
		sysMeta.setText("SimpleSWT.ecore");
		sysMetaURI.setText("http://www.sei.pku.edu.cn/rsa/simpleswt");
		arcName.setText("gui");
		sysName.setText("swt");
		transName.setText("GUI2SWT");
*/
		return parent;
	}
	
	private void initFiles(){
		String prj=workingProject.getText();
		project=root.getProject(prj);
		//project.getFolder("").get
		File[] files=project.getLocation().toFile().listFiles();
		for(int i=0;i<files.length;i++){
			String fn=files[i].getName();
			if(fn.endsWith(".qvt")){
				qvt.add(fn);
			}
			else if(fn.endsWith(".ecore")){
				arcMeta.add(fn);
				sysMeta.add(fn);
			}
			else{
				arcFile.add(fn);
			}
		}
		
		
	}
	
	private void initPackage(){
		Set<String> keys=EPackage.Registry.INSTANCE.keySet();
		ArrayList<String> al=new ArrayList();
		al.addAll(keys);
		Collections.sort(al);
		for(String key:al){
			arcMetaURI.add(key);
			sysMetaURI.add(key);
		}
		arcMeta.select(0);
		sysMeta.select(0);
	}
	
	
	
	private void initWorkingProject(){
		IProject projects[]=root.getProjects();
		for(int i=0;i<projects.length;i++){
			String loc=projects[i].getName();
			workingProject.add(loc);			
		}
		workingProject.select(0);
		initFiles();
	}
	
	protected void okPressed(){
		cc=new CausalConnector();
		cc.setArcMetaURIStr(arcMetaURI.getText());
		cc.setSysMetaURIStr(sysMetaURI.getText());
		String prj=project.getLocation().toPortableString();
		project.getFolder("util");
		try {
			project.refreshLocal(2, null);
		} catch (CoreException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		cc.setUtilDir(prj+"/util/");
		cc.setQvtPath(prj+"/"+qvt.getText());
		cc.setArcPath(prj+"/"+arcFile.getText());
		cc.setTransName(transName.getText());
		cc.setArcName(arcName.getText());
		cc.setSysName(sysName.getText());
		EPackage ep=EPackage.Registry.INSTANCE.getEPackage(sysMetaURI.getText());
		
		
		cc.setSysWrappingPackage(ep);
		Class clazz=ep.getClass();
		clazz.getMethods();
		
		cc.setArcMetaURI(URI.createFileURI(prj+"/"+arcMeta.getText()));
		cc.setSysMetaURI(URI.createFileURI(prj+"/"+sysMeta.getText()));
		
		super.okPressed();
	}
	
	public CausalConnector getCausalConnector(){
		return cc;
	}
	


}
